/*     */ package com.zimbra.cs.account.ldap.upgrade;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import java.io.PrintWriter;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.CommandLineParser;
/*     */ import org.apache.commons.cli.GnuParser;
/*     */ import org.apache.commons.cli.HelpFormatter;
/*     */ import org.apache.commons.cli.Options;
/*     */ import org.apache.commons.cli.ParseException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class LdapUpgrade
/*     */ {
/*  34 */   private static final LdapUpgradePrinter printer = new LdapUpgradePrinter();
/*     */   
/*  36 */   private static String O_HELP = "h";
/*  37 */   private static String O_BUG = "b";
/*  38 */   private static String O_DESCRIBE = "d";
/*  39 */   private static String O_DESCRIBE_ALL = "da";
/*  40 */   private static String O_VERBOSE = "v";
/*     */   
/*     */   private static Options getAllOptions() {
/*  43 */     Options options = new Options();
/*  44 */     options.addOption(O_HELP, "help", false, "print usage");
/*  45 */     options.addOption(O_VERBOSE, "verbose", false, "be verbose");
/*  46 */     options.addOption(O_BUG, "bug", true, "bug number this upgrade is for");
/*  47 */     options.addOption(O_DESCRIBE, "desc", true, "describe this upgrade task");
/*  48 */     options.addOption(O_DESCRIBE_ALL, "descAll", false, "describe all upgrade tasks");
/*  49 */     return options;
/*     */   }
/*     */   
/*     */   private static String getCommandUsage() {
/*  53 */     return LdapUpgrade.class.getCanonicalName() + " <options> [args]";
/*     */   }
/*     */   
/*     */   private static void usage() {
/*  57 */     usage(null, null, null);
/*     */   }
/*     */   
/*     */   static void usage(ParseException e, UpgradeOp upgradeOp, String errMsg)
/*     */   {
/*  62 */     if (e != null) {
/*  63 */       printer.println("Error parsing command line arguments: " + e.getMessage());
/*     */     }
/*     */     
/*  66 */     if (errMsg != null) {
/*  67 */       printer.println(errMsg);
/*  68 */       printer.println();
/*     */     }
/*     */     
/*  71 */     Options opts = getAllOptions();
/*  72 */     PrintWriter pw = printer.getPrintWriter();
/*  73 */     HelpFormatter formatter = new HelpFormatter();
/*  74 */     formatter.printHelp(pw, formatter.getWidth(), getCommandUsage(), null, opts, formatter.getLeftPadding(), formatter.getDescPadding(), null);
/*     */     
/*     */ 
/*     */ 
/*  78 */     pw.flush();
/*  79 */     if (upgradeOp != null) {
/*  80 */       upgradeOp.usage(formatter);
/*     */     }
/*     */   }
/*     */   
/*     */   static UpgradeOp getUpgradeOpUnitTest(String bug) throws ServiceException {
/*  85 */     return getUpgradeOp(bug, printer);
/*     */   }
/*     */   
/*     */   private static UpgradeOp getUpgradeOp(String bug, LdapUpgradePrinter printer) throws ServiceException
/*     */   {
/*  90 */     UpgradeTask upgradeTask = UpgradeTask.getTaskByBug(bug);
/*     */     
/*  92 */     if (upgradeTask == null) {
/*  93 */       printer.println("unrecognized bug number");
/*  94 */       System.exit(1);
/*     */     }
/*     */     
/*  97 */     UpgradeOp upgradeOp = upgradeTask.getUpgradeOp();
/*  98 */     upgradeOp.setPrinter(printer);
/*  99 */     upgradeOp.setBug(bug);
/* 100 */     upgradeOp.setLdapProv(LdapProv.getInst());
/* 101 */     return upgradeOp;
/*     */   }
/*     */   
/*     */   public static void upgrade(String[] args) throws ServiceException
/*     */   {
/* 106 */     printer.println("\n\n--------------");
/* 107 */     printer.print(LdapUpgrade.class.getCanonicalName() + " ");
/* 108 */     for (String arg : args) {
/* 109 */       printer.print(arg + " ");
/*     */     }
/* 111 */     printer.println();
/* 112 */     printer.println("--------------");
/*     */     
/* 114 */     CliUtil.toolSetup();
/*     */     
/* 116 */     CommandLine cl = null;
/*     */     try {
/* 118 */       CommandLineParser parser = new GnuParser();
/* 119 */       Options options = getAllOptions();
/* 120 */       cl = parser.parse(options, args);
/* 121 */       if (cl == null) {
/* 122 */         throw new ParseException("");
/*     */       }
/*     */     } catch (ParseException e) {
/* 125 */       usage(e, null, null);
/* 126 */       System.exit(1);
/*     */     }
/*     */     
/* 129 */     if (cl.hasOption(O_HELP)) {
/* 130 */       usage();
/* 131 */       System.exit(0);
/*     */     }
/*     */     
/* 134 */     if (cl.hasOption(O_DESCRIBE)) {
/* 135 */       String bug = cl.getOptionValue(O_DESCRIBE);
/* 136 */       UpgradeOp upgradeOp = getUpgradeOp(bug, printer);
/* 137 */       upgradeOp.describe();
/* 138 */       System.exit(0);
/*     */     }
/*     */     
/* 141 */     if (cl.hasOption(O_DESCRIBE_ALL)) {
/* 142 */       for (UpgradeTask task : UpgradeTask.values()) {
/* 143 */         String bug = task.getBug();
/* 144 */         UpgradeOp upgradeOp = getUpgradeOp(bug, printer);
/* 145 */         upgradeOp.describe();
/*     */       }
/* 147 */       System.exit(0);
/*     */     }
/*     */     
/* 150 */     if (!cl.hasOption(O_BUG)) {
/* 151 */       usage();
/* 152 */       System.exit(1);
/*     */     }
/*     */     
/* 155 */     boolean verbose = cl.hasOption(O_VERBOSE);
/* 156 */     String bug = cl.getOptionValue(O_BUG);
/*     */     
/* 158 */     UpgradeOp upgradeOp = getUpgradeOp(bug, printer);
/* 159 */     upgradeOp.setVerbose(verbose);
/*     */     
/* 161 */     if (!upgradeOp.parseCommandLine(cl)) {
/* 162 */       System.exit(1);
/*     */     }
/*     */     
/* 165 */     upgradeOp.doUpgrade();
/*     */     
/* 167 */     printer.println("\n\n--------------");
/* 168 */     printer.println("done " + bug);
/* 169 */     printer.println("--------------");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void main(String[] args)
/*     */     throws ServiceException
/*     */   {
/* 180 */     upgrade(args);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/upgrade/LdapUpgrade.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */